﻿#include "pch.h"

#include <array>
#include <chrono>
#include <typeindex>
#include <typeinfo>
#include <__msvc_filebuf.hpp>
using  std::cout;
using  std::endl;
class Entity {
public:
	float X, Y;

	Entity() {
		std::cout << "Gou zhao" << std::endl;
	}

	~Entity() {
		std::cout << "Xi Gou " << std::endl;
	}

	virtual std::string GetName() = 0;
	// virtual std::string GetName2() = 0;

	void PrintXY() {
		std::cout << "X: " << X << ",Y:" << Y << std::endl;
	}
};

class Player :Entity {
	std::string GetName() override {
		return "player";
	}
public:
	float GetX() {
		return X;
	}

	float ConstGetX() const {
		return X;
	}
};

void GX() {
	Player p;
}

void PrintPlayer(const Player& p) {
	float res = p.ConstGetX();
	std::cout << res << std::endl;

}

void PrintPlayer(Player& p) {
	std::cout << p.GetX() << std::endl;

}

TEST(Learn, GouZhao) {
	GX();
}

TEST(Learn, string) {
	const char* str0 = "CPPSB0";
	"asd";
	std::cout << str0 << std::endl;
	char str1[6] = { 'C','P','P','S','B',0 };
	std::cout << str1 << std::endl;
}

TEST(Learn, constEx) {
	const int x = 10;
	int* const ptr = new int;
	std::cout << "*ptr: " << *ptr << std::endl;;
	// ptr = (int*)(&x);
	*ptr = 20;
	std::cout << "x: " << *ptr << std::endl;;
}

std::vector<int> newV() {
	std::vector<int> ins;
	ins.push_back(1);
	ins.push_back(2);
	ins.push_back(3);
	return ins;
}

TEST(Learn, vEx) {
	std::vector<int>* p;
	for (const auto vs = newV(); auto v : vs) {
		cout << v << endl;
	}
	cout << "end" << endl;
}

#include <iostream>

class MyClass {
public:
	MyClass(int value) : data(value) {
		std::cout << "MyClass Constructor" << std::endl;
	}

	~MyClass() {
		std::cout << "MyClass Destructor" << std::endl;
	}

	int getData() const {
		return data;
	}

	// MyClass(const MyClass& clone) = delete;
	MyClass& operator=(const MyClass& other) = delete;


private:
	int data;
};

MyClass createAndReturnObject(int value) {
	if (value > 100) {
		MyClass obj(value - 100);
		return obj;
	}
	else {
		MyClass obj(value);
		return obj;
	}

}

void FZ(const MyClass& mc) {
	cout << mc.getData() << endl;
}

TEST(Learn, MyC) {

	auto result = createAndReturnObject(142);
	FZ(result);
	// 在这里，局部对象已被销毁，将无法访问
	// 尝试访问将导致未定义行为
	std::cout << "Data: " << result.getData() << std::endl;

}

void Fn() {
	cout << "hw" << endl;
}

TEST(Learn, fnPtr) {
	auto fn = Fn;
	auto tid = std::type_index(typeid(fn));
	cout << tid.name() << endl;
	fn();
}


TEST(Learn, chrono) {
	using namespace std::literals::chrono_literals;
	auto start = std::chrono::high_resolution_clock::now();
	std::this_thread::sleep_for(1s);
	auto end = std::chrono::high_resolution_clock::now();
	auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
	cout << duration.count() << endl;
}


struct Timer {
	std::chrono::time_point<std::chrono::high_resolution_clock> start, end;

	//构造函数构造一个启动计时器
	Timer() {
		start = std::chrono::high_resolution_clock::now();
	}

	~Timer() {
		end = std::chrono::high_resolution_clock::now();
		auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
		std::cout << duration.count() << " ms\n";
	}

};


TEST(Learn, timer) {
	Timer t;
	using namespace std::literals::chrono_literals;
	auto start = std::chrono::high_resolution_clock::now();
	std::this_thread::sleep_for(1s);
	auto end = std::chrono::high_resolution_clock::now();
	auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
	cout << duration.count() << endl;
}

void createArray(int x) {
	
}
std::tuple<std::string ,int> createSI() {
	return { "asd",12 };
}
TEST(Learn, JG) {
	auto [str, i] = createSI();
	cout << str << endl;
	cout << i << endl;
}